package com.mm.gbicc.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mm.gbicc.bean.Menu;
import com.mm.gbicc.dao.conn.OracleConn;
import com.mm.gbicc.util.DateTimeUtils;

public class FileDao {
	
	public boolean addMenu(Menu menu){
		Connection connection = null;
		Statement sta = null;
		ResultSet rs = null;
		boolean bool = false;
		try {
			connection = OracleConn.getInstance().newConn();
			connection.setAutoCommit(false);
			sta = connection.createStatement();
			sta.execute("select seq_id.nextval from dual"); 
			//再执行就好了 
			//select seq_id.CURRVAL from dual  ;
			String max_id = "select seq_id.currval as idStr from dual";
			rs = sta.executeQuery(max_id);
			String idstrd = "";
			while(rs.next()){
				System.out.println(rs.getInt(1));
				idstrd = rs.getString(1);
			}
			String time = DateTimeUtils.now2StrDateTime();
			String insert_sql = "insert into t_sys_menu (IDSTRD,NAME_CN,PARENT_ID,URL,NOTE,SORT,CREATE_USER_ID," +
					"CREATE_TIME,UPDATE_USER_ID,UPDATE_TIME,ISDELETE,MENU_TYPE,IS_REPORT) " +
					"values ("+idstrd+",'"+menu.getName_cn()+"','"+menu.getParent_id()+"','"+menu.getUrl()+"'," +
					"'"+menu.getNote()+"',1,"+menu.getCreate_user_id()+",'"+time+"',1,'"+time+"',0,1,1)";
			System.out.println(insert_sql);
			sta.addBatch(insert_sql);
			sta.executeBatch();
			sta.clearBatch();
			String sql ="select role_id from t_sys_rolemenu where menu_id = '"+menu.getParent_id()+"'";
			rs = sta.executeQuery(sql);
			while(rs.next()){
				sta.addBatch("insert into t_sys_rolemenu (role_id,menu_id) values('"+rs.getString(1)+"','"+idstrd+"')");
			}
			sta.executeBatch();
			sta.clearBatch();
			connection.commit();
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(sta!=null)
					sta.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bool;
	}
	
	
	public List<Menu> getmenuS(String parent_id,String status){
		Connection connection = null;
		Statement sta = null;
		ResultSet rs = null;
		List<Menu> menus = new ArrayList<Menu>();
		try {
			connection = OracleConn.getInstance().newConn();
			//connection.setAutoCommit(false);
			sta = connection.createStatement();
			String sql_s = "";
			if("".equals(status))
				sql_s = "select * from t_sys_menu where parent_id = '"+parent_id+"' and url = '/' order by sort ";
			else
				sql_s = "select * from t_sys_menu where parent_id = '"+parent_id+"' order by sort, create_time desc ";
			//System.out.println(sql_s);
			rs = sta.executeQuery(sql_s);
			Menu menu = null;
			while(rs.next()){
				menu = new Menu();
				menu.setIdStrd(rs.getString(1));
				menu.setName_cn(rs.getString(2));
				menu.setParent_id(rs.getString(3));
				
				menu.setUrl(rs.getString(4));
				menus.add(menu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(sta!=null)
					sta.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return menus;
	}
	public static void main(String[] args) {
		FileDao dao = new FileDao();
		List<Menu> menus = dao.getmenuS("1000182855","");
		StringBuilder sb = new StringBuilder();
		
		dao.getForJson(dao,sb, menus);
		
		System.out.println(sb.toString());
	}
	
	public static void get(FileDao dao,StringBuilder sb,Menu menu){
		//dao = new FileDao();
		List<Menu> menus2 = dao.getmenuS(menu.getIdStrd(),"");
		sb.append("<li>\n");
		if(menus2.isEmpty()){
			//System.out.println(menu.getName_cn());
			sb.append("<span><a id='"+menu.getIdStrd()+"' href=javascript:void(checked(").append(menu.getIdStrd()).append("))>"+menu.getName_cn()+"</a>").append("</span>\n");
		}else{
			sb.append("<span>"+menu.getName_cn()+"</span>\n");
			List<Menu> menus3 = null;
			for (Menu menu2 : menus2) {
				//System.out.println(menu2.getName_cn());
				menus3 = dao.getmenuS(menu2.getIdStrd(),"");
				sb.append("<ul><li><span>\n");
				if(!menus3.isEmpty()){
					sb.append(menu2.getName_cn()).append("</span>");
					for (Menu menu3 : menus3) {
						sb.append("<ul>");
						get(dao,sb,menu3);
						sb.append("</ul>");
					}
				}else{
					sb.append("<a id='"+menu2.getIdStrd()+"' href=javascript:void(checked(").append(menu2.getIdStrd()).append("))>"+menu2.getName_cn()+"</a>").append("</span>");
				}
				
				sb.append("</li></ul>\n");
			}
		}
		sb.append("</li>");
	}
	/*{
		"text":"Languages",
		"state":"closed",
		"children":[{
			"id":"j1",
			"text":"Java"
		},{
			"id":"j2",
			"text":"C#"
		}]
	}*/
	public String getForJson(FileDao dao,StringBuilder sb,List<Menu> menus){
		String text = "";
		sb.append("[");
		for (int i = 0; i < menus.size(); i++) {
			Menu menu = menus.get(i);
			sb.append("{");
			List<Menu> list = dao.getmenuS(menu.getIdStrd(),"1");
			
			String url = menu.getUrl();
			//System.out.println("--->"+url);
			if("/".equals(url)){
				text = menu.getName_cn()+" (文件夹)";
			}else{
				text = "<input class='tree-checkbox' type='checkbox' value='"+url.substring(url.lastIndexOf("/"))+"' id='"+menu.getIdStrd()+"'>"+ menu.getName_cn()+url.substring(url.lastIndexOf("."))+" (文件)";
			}
			if(list!=null&&list.size()>0)
				sb.append("\"id\":"+menu.getIdStrd()+",\"text\":\""+text+"\",\"state\":\"closed\",\"url\":\""+url+"\"");
			else
				sb.append("\"id\":"+menu.getIdStrd()+",\"text\":\""+text+"\",\"url\":\""+url+"\"");
			if(i<(menus.size()-1))
				sb.append("},");
			else
				sb.append("}");
		}
		sb.append("]");
		return sb.toString();
	}
	
	
	public boolean deleteMenu(String[] ids){
		Connection connection = null;
		Statement sta = null;
		ResultSet rs = null;
		boolean bool = false;
		try {
			connection = OracleConn.getInstance().newConn();
			connection.setAutoCommit(false);
			sta = connection.createStatement();
			
			for (String id : ids) {
				sta.addBatch("delete from t_sys_rolemenu where menu_id = '"+id+"'");
				sta.addBatch("delete from t_sys_orgmenu where menu_id = '"+id+"'");
				System.out.println("delete from t_sys_menu where idstrd='"+id+"'");
				sta.addBatch("delete from t_sys_menu where idstrd='"+id+"'");
			}
			sta.executeBatch();
			sta.clearBatch();
			connection.commit();
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(sta!=null)
					sta.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bool;
	}
}
